﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAGuiLib.Core.Utilities;

namespace XNAGuiLib.Core.Components
{
    public class Background : GIComponent
    {
        #region Background Protected Field - _rcm
        protected ResourceContentManager _rcm;
        #endregion

        #region Background Protected Field - _assetPrefix
        protected string _assetPrefix;
        #endregion


        #region Background Public Field - Bounds
        /// <summary>
        /// gets or sets the bounds of the background
        /// </summary>
        public new Vector2 Bounds
        {
            get { return new Vector2(this._width, this._height); }
            set { this.Resize((int)value.X, (int)value.Y); }
        }
        #endregion

        #region Background Public Field - BackgroundTile
        /// <summary>
        /// gets or sets the background component
        /// </summary>
        public GIComponent BackgroundTile
        {
            get { return this._gics[0]; }
            set { this._gics[0] = value; }
        }
        #endregion


        #region Background Public Field - BackgroundColor
        public Color BackgroundColor
        {
            get { return this._gics[0].ContentComponents[0].ContTexture.ModulationColor;}
            set { this._gics[0].ContentComponents[0].ContTexture.ModulationColor = value; }
        }
        #endregion

        #region Background Public Field - BackgroundDecorationColor
        public Color BackgroundDecorationColor
        {
            get { return  this._gics[1].ContentComponents[0].ContTexture.ModulationColor; }
            set { this._gics[1].ContentComponents[0].ContTexture.ModulationColor = value; }
        }
        #endregion


        #region Background Public Field - Decoration
        /// <summary>
        /// gets or sets the decoration component
        /// </summary>
        public GIComponent Decoration
        {
            get { return this._gics[1]; }
            set { this._gics[1] = value; }
        }
        #endregion


        #region Background Public Field - UseDecoration
        /// <summary>
        /// gets or sets if the background decoration should be used
        /// </summary>
        public bool UseDecoration
        {
            get
            {
                if (this.Decoration != null)
                    return this.Decoration.Visible;
                else return false;
            }
            set
            {
                if (this.Decoration != null)
                    this.Decoration.Visible = value;
            }
        }
        #endregion


        /* Component mapping for the Background Element
         * ============================================
         * [0]  Background Tile
         */

        public Background(Game game, ResourceContentManager rcm, string assetPrefix, int width, int height)
            :base(game)
        {
            this._rcm = rcm;
            this._assetPrefix = assetPrefix;
            this._width = width;
            this._height = height;

            GIComponent background = this.CreateGIC("background", new Rectangle(0, 0, this._width, this._height));
            this.Add(background);

            GIComponent decoration = this.CreateGIC("decoration");
            decoration.ContentComponents[0].Rectangle = new Rectangle(0, 0, width, decoration.Height);
            decoration.RelativePosition = new Vector2(0, height - decoration.Height);
            this.Add(decoration);
        }


        #region Background Protected Method - Resize(int width, int height)
        /// <summary>
        /// resizes the background and its decoration
        /// </summary>
        /// <param name="width">width to resize the background to</param>
        /// <param name="height">height to resize the background to</param>
        protected override void Resize(int width, int height)
        {
            this._width = width;
            this._height = height;

            this.BackgroundTile.ContentComponents[0].Rectangle = new Rectangle(0, 0, this._width, this._height);
            this.BackgroundTile.Bounds = new Vector2(this._width, this._height);
            this.Decoration.ContentComponents[0].Rectangle = new Rectangle(0, 0, this._width, this.Decoration.Height);
            this.Decoration.RelativePosition = new Vector2(0, this._height - this.Decoration.Height);
            this.Decoration.Bounds = new Vector2(this._width, this.Decoration.Height);
        }
        #endregion

        #region Background Protected Method - CreateGIC(string asset)
        /// <summary>
        /// creats a new GIComponent with the given asset of the ResourceContentManager
        /// </summary>
        /// <param name="asset">the name of the asset to load</param>
        /// <returns>the created GIComponent</returns>
        protected GIComponent CreateGIC(string asset)
        {
            ContentComponent cc = new ContentComponent(_rcm.Load<Texture2D>(this._assetPrefix + asset));
            GIComponent gic = Provider.GetInstance(this._game).CreateGIC();
            gic.Add(cc);

            return gic;
        }
        #endregion

        #region Background Protected Method - CreateGIC(string asset, Rectangle rect)
        /// <summary>
        /// creates a new GIComponent with the given asset scaled to the given rectangle
        /// </summary>
        /// <param name="asset">the name of the asset to load</param>
        /// <param name="rect">the rectangle to which the texture is scaled (width & height)</param>
        /// <returns>the created GIComponent</returns>
        protected GIComponent CreateGIC(string asset, Rectangle rect)
        {
            ContentComponent cc = new ContentComponent(_rcm.Load<Texture2D>(this._assetPrefix + asset), rect);
            GIComponent gic = Provider.GetInstance(this._game).CreateGIC();
            gic.Add(cc);

            return gic;
        }
        #endregion


        #region Background Public Method - Opaque(int percentage)
        /// <summary>
        /// set the opaque of the whole background (only child-GICs)
        /// </summary>
        /// <param name="percentage">degree of visibility/opaque</param>
        public new void Opaque(int percentage)
        {
            if (percentage >= 0 && percentage <= 100)
            {
                foreach (GIComponent gic in _gics)
                {
                    gic.Opaque(percentage);
                }
            }
        }
        #endregion
    }
}
